﻿/// <reference path="Brick.js" />

var engine = (function () {
    var HORIZONTAL_MOVEMENT = 5;
    var difficulty = 8;
    var stage = document.createElement("div");
    stage.id = "testStage";
    stage.style.width = 600 + "px";
    stage.style.height = 800 + "px";

    var lastPressedKey;
    var lastBouncedPosition;
    var nextTopPosition = 210;
    
    var moved = false;

    var heroPlayer;
    var bricks = [];

    var horizontalMovement = 5;

    var heroHorizontal = 0;
    var heroVertical = -6;

    var beginPlaying;

    function onArrowDown(ev) {
        if (!ev) ev = window.event;
        var pressedKey = ev.keyCode;

        if (pressedKey == 37) {
            heroHorizontal = -horizontalMovement;
            lastPressedKey = 37;
        }
        else if (pressedKey == 39) {
            heroHorizontal = horizontalMovement;
            lastPressedKey = 39;
        }
        horizontalMovement += 0.5;
    }

    function onArrowUp(ev) {
        if (!ev) ev = window.event;
        var offKey = ev.keyCode;

        if (offKey == lastPressedKey) {
            heroHorizontal = 0;
        }
        horizontalMovement = HORIZONTAL_MOVEMENT;
    }

    var play = function () {
        moveBall();
        removeAll();
    }

    function moveBall() {
        for (var i = 0, len = bricks.length; i < len; i += 1) {
            var brick = bricks[i];
            if (brick.bouncable && heroVertical < 0) {
                if (heroPlayer.center.Y <= brick.bounding.top && heroPlayer.center.Y > brick.bounding.top - 20 && heroPlayer.center.LX <= brick.bounding.right &&
                    heroPlayer.center.RX >= brick.bounding.left) {
                    lastBouncedPosition = brick.bounding.top;
                    nextTopPosition = lastBouncedPosition + 200;
                    heroVertical = Math.abs(heroVertical);
                    generateBricks();
                    moveAllDown(100);
                }
            }
        }

        if (heroPlayer.center.Y > nextTopPosition) {
            heroVertical = -1 * heroVertical;
        }

        var sign = -1;
        if (heroVertical > 0) {
            sign = 1;
        }

        if (heroPlayer.bottom > nextTopPosition - 30) {
            heroVertical = 2 * sign;
        }
        else if (heroPlayer.bottom < lastBouncedPosition + 30) {
            heroVertical = 8 * sign;
        }
        else {
            heroVertical = 6 * sign;
        }

        heroPlayer.move(heroHorizontal, heroVertical);

        var widthString = stage.style.width.substr(0, stage.style.width.length - 2);
        var width = parseInt(widthString);
        if (heroPlayer.left < -20) {
            heroPlayer.hero.style.left = width - 20 + "px";
            horizontalMovement = HORIZONTAL_MOVEMENT;
        }
        if (heroPlayer.left > width - 20) {
            heroPlayer.hero.style.left = -20 + "px";
            horizontalMovement = HORIZONTAL_MOVEMENT;
        }

        if (heroPlayer.bottom < -50) {
            alert("Too bad you lost");
            clearInterval(beginPlaying);
        }
    }

    var start = function () {
        document.body.appendChild(stage);

        if (!document.body.addEventListener) {
            document.body.attachEvent("keydown", onArrowDown);
        }
        else {
            document.body.addEventListener("keydown", onArrowDown, false);
        }

        if (!document.body.addEventListener) {
            document.body.attachEvent("keyup", onArrowUp);
        }
        else {
            document.body.addEventListener("keyup", onArrowUp, false);
        }

        createStartLayer();

        beginPlaying = setInterval(function () { play() }, 10);
    }

    function createStartLayer() {
        for (var i = 0; i < 6; i++) {
            var brick = new Brick();
            brick.body.style.left = i * 100 + "px";
            brick.body.style.bottom = 0 + "px";
            brick.bounding.left = i * 100; //-------
            brick.bounding.right = brick.bounding.left + 100; //-------
            brick.bounding.top = 20; //------
            stage.appendChild(brick.body);
            bricks.push(brick);
        }

        var firstBrick = new Brick();
        firstBrick.body.style.left = 100 + "px";
        firstBrick.body.style.bottom = 200 + "px";
        firstBrick.bounding.left = 100;
        firstBrick.bounding.right = firstBrick.bounding.left + 100;
        firstBrick.bounding.top = 220;
        stage.appendChild(firstBrick.body);
        bricks.push(firstBrick);

        var secondBrick = new Brick();
        secondBrick.body.style.left = 350 + "px";
        secondBrick.body.style.bottom = 450 + "px";
        secondBrick.bounding.left = 350;
        secondBrick.bounding.right = secondBrick.bounding.left + 100;
        secondBrick.bounding.top = 470;
        stage.appendChild(secondBrick.body);
        bricks.push(secondBrick);

        var thirdBrick = new Brick();
        thirdBrick.body.style.left = 150 + "px";
        thirdBrick.body.style.bottom = 500 + "px";
        thirdBrick.bounding.left = 150;
        thirdBrick.bounding.right = thirdBrick.bounding.left + 100;
        thirdBrick.bounding.top = 520;
        stage.appendChild(thirdBrick.body);
        bricks.push(thirdBrick);

        var forthBrick = new Brick();
        forthBrick.body.style.left = 400 + "px";
        forthBrick.body.style.bottom = 650 + "px";
        forthBrick.bounding.left = 400;
        forthBrick.bounding.right = forthBrick.bounding.left + 100;
        forthBrick.bounding.top = 670;
        stage.appendChild(forthBrick.body);
        bricks.push(forthBrick);
    }

    function moveAllDown(maxTimes) {
        for (var i = 0, len = bricks.length; i < len; i += 1) {
            bricks[i].moveDown(maxTimes);
        }
    }

    function removeAll() {
        for (var i = 0; i < bricks.length; i += 1) {
            if (bricks[i].bounding.top < 0) {
                stage.removeChild(bricks[i].body);
                bricks.splice(i, 1);
            }
        }
    }

    var addHero = function (hero) {
        //heroes.push(hero);
        heroPlayer = hero;
        stage.appendChild(hero.hero);
    }

    var generateBricks = function () {
        var bottoms = [800, 820, 840, 860, 880, 900];
        var lefts = [0, 50, 150, 200, 250, 300, 350, 400, 450, 500];

        for (var i = 0; i < 2; i++) {
            var index = Math.floor(Math.random() * 6);
            var brickBottom = bottoms[index];
            index = Math.floor(Math.random() * 9);
            var brickLeft = lefts[index];

            var brick = new Brick();
            brick.body.style.left = brickLeft + "px";
            brick.body.style.bottom = brickBottom + "px";
            brick.bounding.left = brickLeft;
            brick.bounding.right = brick.bounding.left + 100;
            brick.bounding.top = brickBottom + 20;
            stage.appendChild(brick.body);
            bricks.push(brick);
        }
    }


    function Brick() {
        this.bouncable = true;
        this.body = document.createElement("div");
        this.body.className = "brick";
        var height = 20;
        var width = 92;
        this.body.style.width = width + "px";
        this.body.style.height = height + "px";
        var proxyBody = this.body;
        var speed = 2;

        //var res1 = Math.floor(Math.random() * (700 - width)) + width;
        //this.body.style.left = res1 + "px";

        //var res2 = Math.floor(Math.random() * (500 - 100)) + 100;
        //this.body.style.bottom = res2 + "px";
        var res1 = 0;
        var res2 = 0;

        this.bounding = { top: res2 + height, bottom: res2, left: res1, right: res1 + width };
        var proxyBounding = this.bounding;

        function descend(thisBody, pBounding) {
            var currentBottom = thisBody.style.bottom.substr(0, thisBody.style.bottom.length - 2);
            currentBottom -= speed;
            thisBody.style.bottom = currentBottom + "px";

            pBounding.top -= speed;
            pBounding.bottom -= speed;
            //heroPlayer.move(0, -2);
        };

        this.moveDown = function (maxTimes) {
            var times = 0;
            var moving = setInterval(function () {
                times++;

                descend(proxyBody, proxyBounding);
                if (times == maxTimes) {
                    clearInterval(moving);
                    times = 0;
                }
            }, 0);
        };
    }

    return {
        start: start,
        addHero: addHero
    }
}());